using System.IO;
using AesEverywhere;
using UnityEngine;

public class SaveLoad
{
	private static string fileExt = ".data";

	private static string filePath = Application.persistentDataPath;

	private static string encryptionKey = Config.encryptionKey;

	private static bool encryption = true;

	private static bool debug = false;

	public static void SaveString(string identify, string content)
	{
		// if (debug)
		// {
		// 	Debug.Log("SaveLoad Save " + identify + " to " + GetFilePath(identify));
		// }
		// StreamWriter streamWriter = new StreamWriter(GetFilePath(identify), append: false);
		// content = (encryption ? Encrypt(content) : content);
		// streamWriter.WriteLine(content);
		// streamWriter.Close();
		PlayerPrefs.SetString(identify, content);
		PlayerPrefs.Save();
	}

	public static string LoadString(string identify)
	{
		// if (debug)
		// {
		// 	Debug.Log("SaveLoad Load " + identify + " From " + GetFilePath(identify));
		// }
		// if (!File.Exists(GetFilePath(identify)))
		// {
		// 	if (debug)
		// 	{
		// 		Debug.Log("File not Exists " + GetFilePath(identify));
		// 	}
		// 	return null;
		// }
		// StreamReader streamReader = new StreamReader(GetFilePath(identify));
		// string text = streamReader.ReadToEnd();
		// text = (encryption ? Decrypt(text) : text);
		// streamReader.Close();
		// return text;
		if (!PlayerPrefs.HasKey(identify))
		{
			return null;
		}
		return PlayerPrefs.GetString(identify);
	}

	public static void DeleteString(string identify)
	{
		// if (!File.Exists(GetFilePath(identify)))
		// {
		// 	if (debug)
		// 	{
		// 		Debug.Log("File not Exists " + GetFilePath(identify));
		// 	}
		// }
		// else
		// {
		// 	File.Delete(GetFilePath(identify));
		// }
		if (!PlayerPrefs.HasKey(identify))
		{
			Debug.Log("File not Exists " + GetFilePath(identify));
		}
		PlayerPrefs.DeleteKey(identify);
	}

	public static string GetFilePath(string identify)
	{
		return Path.Combine(Application.persistentDataPath, identify + fileExt);
	}

	public static string Encrypt(string clearText)
	{
		string text = new AES256().Encrypt(clearText, encryptionKey);
		if (debug)
		{
			Debug.Log("Encrypt: " + clearText);
		}
		if (debug)
		{
			Debug.Log("CipherText: " + text);
		}
		return text;
	}

	public static string Decrypt(string cipherText)
	{
		string text = new AES256().Decrypt(cipherText, encryptionKey);
		if (debug)
		{
			Debug.Log("Decrypt: " + cipherText);
		}
		if (debug)
		{
			Debug.Log("ClearText: " + text);
		}
		return text;
	}
}
